package ch.unisi.inf.java.project.riskbami.model;

import ch.unisi.inf.java.project.riskbami.view.*;

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;

/**
 * Class to handle the loading of maps.
 * 
 * @author babazadm@lu.unisi.ch
 */
public final class MapLoader extends JComponent
{
    // The current width and height of this map
    private int width, height;
    private String pathFile;
    private static final JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
    private final SetupFrame frame;

    // An internal image buffer that is used for painting. For
    // actual display, this image buffer is then copied to screen.
    private ImageIcon panelImage;
    private BufferedImage bufferedMap;

    /**
     * Create a new, empty ImagePanel.
     */
    public MapLoader(final SetupFrame frame)
    {
        width = 600;    // arbitrary size for empty panel
        height = 400;
        panelImage = null;
        this.frame = frame;
    }

    /**
     * Set the image that this panel should show.
     * 
     * @param image  The image to be displayed.
     */
    public final void setImage(final ImageIcon image)
    {
        if(image != null) {
            height = image.getIconHeight();
            width = image.getIconWidth();
            panelImage = image;
            frame.setPixelMatrix(getPixelMatrix());
        }
    }
    
    // The following methods are redefinitions of methods
    // inherited from superclasses.
    
    /**
     * Tell the layout manager how big we would like to be.
     * (This method gets called by layout managers for placing
     * the components.)
     * 
     * @return The preferred dimension for this component.
     */
    public Dimension getPreferredSize()
    {
        return new Dimension(width, height);
    }
    /**
     * Gets the image from a file.
     */
    public ImageIcon getImage()
    {
        final int returnVal = fileChooser.showOpenDialog(null);

        if(returnVal != JFileChooser.APPROVE_OPTION) {
            return null;  // cancelled
        }
        final File selectedFile = fileChooser.getSelectedFile();
        pathFile = selectedFile.getAbsolutePath();
        final ImageIcon currentImage = loadImage(selectedFile);
        setImage(currentImage);
        return currentImage;
    }
    
    /**
     * Loads the image
     */
    public final ImageIcon loadImage(final File imageFile)
    {
        try {
            final BufferedImage bufferedImage = ImageIO.read(imageFile);
            bufferedMap = bufferedImage;
            final ImageIcon image = new ImageIcon(bufferedImage);
            if(image == null) {
                // we could not load the image - probably invalid file format
                return null;
            }
            return image;
        }
        catch(final IOException exc) {
            return null;
        }
    }
    
    /**
     * Load File
     */
    public FileInputStream getFile()
    {
        final int returnVal = fileChooser.showOpenDialog(null);

        if(returnVal != JFileChooser.APPROVE_OPTION) {
            return null;  // cancelled
        }
        final File selectedFile = fileChooser.getSelectedFile();
        final String fileInString = selectedFile.getPath();
        try{
            FileInputStream file = new FileInputStream(fileInString);
            return file;
        }
        catch(final IOException exc) {
        return null;
        }   
     }

    /**
     * Returns a matrix of pixels
     */
    public final Raster getPixelMatrix() {
        return bufferedMap.getData();
    }
    
    /**
     * Returns the bufferedMap of type BufferedImage
     */
    public final BufferedImage getBufferedMap() {
        return bufferedMap;
    }
    
    /**
     * Returns the path of the chosen file.
     */
    public final String getPath() {
        return pathFile;
    }
}
